package com.example.calculation3

import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.EditText
import android.widget.ImageView
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.widget.doAfterTextChanged

class MainActivity : AppCompatActivity() {
    val TAG = "MainActivity"
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)
        val tv1 = findViewById<TextView>(R.id.tv1)
        val tv2 = findViewById<TextView>(R.id.tv2)
        val tv3 = findViewById<TextView>(R.id.tv3)
        val tv4 = findViewById<TextView>(R.id.tv4)
        val tv5 = findViewById<TextView>(R.id.tv5)
        val tv6 = findViewById<TextView>(R.id.tv6)
        val tv7 = findViewById<TextView>(R.id.tv7)
        val tv8 = findViewById<TextView>(R.id.tv8)
        val tv9 = findViewById<TextView>(R.id.tv9)
        val tv0 = findViewById<TextView>(R.id.tv0)
        val tv00 = findViewById<TextView>(R.id.tv00)
        val tvTubiao = findViewById<TextView>(R.id.tvTubiao)
        val tvClear = findViewById<TextView>(R.id.tvClear)
        val tvAdd = findViewById<TextView>(R.id.tvAdd)
        val ivDelete = findViewById<ImageView>(R.id.ivDelete)
        val etProcess = findViewById<EditText>(R.id.etProcess)
        val tvResult = findViewById<TextView>(R.id.tvResult)
//        var currentInput = ""//储存输入的数字的字符串
        var splitNums = ""//储存分割后的字符串
        var totalResult = 0

        val arrayList1 = ArrayList<TextView>()
        arrayList1.add(tv1)
        arrayList1.add(tv2)
        arrayList1.add(tv3)
        arrayList1.add(tv4)
        arrayList1.add(tv5)
        arrayList1.add(tv6)
        arrayList1.add(tv7)
        arrayList1.add(tv8)
        arrayList1.add(tv9)
        arrayList1.add(tv0)
        arrayList1.add(tv00)
        arrayList1.add(tvAdd)
        arrayList1.forEach() { textview ->
            textview.setOnClickListener {

                etProcess.append(textview.text.toString())

//                tvTubiao.visibility = View.VISIBLE
//                val number = textview.text.toString()
//
////                currentInput += number
//                val parts = currentInput.split(tvAdd.text.toString())
//                var sum = 0
//                for (part in parts){
//                    val  number = part.toInt()
//                    sum += number
//                }
//                tvResult.setText(sum.toString())
////                if (textview == tvAdd){
////                    currentInput = ""//当点了+之后，储存数字的变量变成空的
////                }
            }

        }
        tvAdd.setOnClickListener {
//            if (currentInput.isNotEmpty()){//只有当输入了数字以后才能显示+

            etProcess.append("+")

//            }

        }
        tvClear.setOnClickListener {
//            currentInput = ""
//            currentInput.split("+")
//            totalResult = 0
//            tvResult.text = ""
            etProcess.text.clear()
//            tvTubiao.visibility = View.INVISIBLE
        }



//        etProcess.text
        etProcess.doAfterTextChanged {
            val parts = it.toString().split(tvAdd.text.toString())  //"11", ""
            var sum = 0
            for (part in parts) {
                val number = part.toIntOrNull()//解决加号转成字符串会变成空的问题
                if(number != null) {
                    sum += number
                }
            }
            tvResult.setText(sum.toString())

        }
        ivDelete.setOnClickListener{
//            val delete = etProcess.toString().substring(0,etProcess.toString().length-1)
            if (etProcess.text.length > 0){
                etProcess.editableText.delete(etProcess.text.length - 1, etProcess.text.length)

            }
        }


    }
}